package com.example.registroadmin;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import com.google.gson.Gson;

import domain.AdministradorVO;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {
	
	private static final String METHOD_NAME = "";
	private static final String NAMESPACE = "";
	private static final String SOAP_ACTION = "";
	private static final String URL = "";
	
	TextView textResp;
	EditText usser, pass;
	Button bEnviar, bCancelar;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		textResp=(TextView) findViewById(R.id.textResp);
		usser=(EditText) findViewById(R.id.editUsser);
		pass=(EditText) findViewById(R.id.editPass);
		bEnviar=(Button) findViewById(R.id.btnEnviar);
		
		bEnviar.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String resp=registroAdmin(usser.getText().toString(),
						                  pass.getText().toString());
				textResp.setText(resp);
				
			}
		});
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	public String registroAdmin(String usser, String pass){
		
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		//GSON
		AdministradorVO admin=new AdministradorVO();
		admin.setUser(usser);
		admin.setPassword(pass);
		final Gson gson=new Gson();
		String send=gson.toJson(admin);
		
		//parametros
		PropertyInfo jSonPI=new PropertyInfo();
		jSonPI.setName("adminRegJSON");
		jSonPI.setValue(send);
		jSonPI.setType(String.class);
		request.addProperty(jSonPI);
		
		//Envio
		
		 SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
         envelope.dotNet = true;
         envelope.setOutputSoapObject(request);
         HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
         
         try {
             androidHttpTransport.call(SOAP_ACTION, envelope);
             SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
             Log.i("Webservice Output", response.toString());

             return response.toString();

     } catch (Exception e) {
             e.printStackTrace();
             Log.i("Catchhh", " " + e);
     }

     return null;
}
	
}
